const app = getApp();
const config = require("../../../config.js");
var request_api = require('../../../utils/require_api.js')
Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function(options) {},
  bindGetUserInfo: function(e) { //点击授权登录  把用户基本信息保存到本地存储中，斌且调用登录方法获取openid
    var n = this;
    wx.getUserInfo({
      success: function(t) {
        app.globalData.userInfo = t.userInfo,
          wx.setStorageSync("NICK_NAME", t.userInfo.nickName),
          wx.setStorageSync("USER_IMAGE", t.userInfo.avatarUrl),
          wx.setStorageSync("PROVINCE", t.userInfo.province),
          wx.setStorageSync("CITY", t.userInfo.city),
          n.login().then(function() {
            console.log("成功登陆");
          });
      },
      fail: function(t) {
        wx.showModal({
          title: '警告',
          content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
          showCancel: false,
          confirmText: '返回授权',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击了“返回授权”')
            }
          }
        })
      }
    });
  },

  login: function() { //授权登录获取openid方法
    var t = this;
    return wx.showLoading({
        title: "授权登录中..."
      }),
      new Promise(function(o, e) {
        wx.login({
          success: function(e) {
            e.code ? (
              request_api.send(config.uri+'/api/wx/Login4WX', { //获取openid
                "code": e.code,
                'openid':'',
                'platform': config.wxcode
                }, //需要传的 data
                function(res) {
                  console.log(res, "this is authorize  the code", e.code);
                  if (res.data.errcode == 0) {
                    wx.setStorageSync("openid", res.data.openid); //把获取到的数据保存到本地缓存中，便于其他页面使用
                    wx.setStorageSync("company_key", res.data.company_key);//公司key
                    wx.setStorageSync("show_company", res.data.company_name);//公司名字
                    wx.setStorageSync("entityName", res.data.entityName); //店铺名字
                    wx.setStorageSync("entity_code", res.data.entity_code); //获取店铺编码
                    wx.setStorageSync("name", res.data.name); //获取用户名字
                    wx.setStorageSync("phone", res.data.phone);//获取电话号码
                    wx.setStorageSync("token", res.data.token); //获取token
                    wx.setStorageSync("user_key", res.data.user_key);//用户的key
                    wx.setStorageSync("ekey", res.data.ekey);//店铺的ekey
                    wx.redirectTo({
                      url: '../../index/index',
                    })
                    wx.getSetting({
                      success(res) {
                        if (!res.authSetting['scope.writePhotosAlbum']) {
                          wx.authorize({
                            scope: 'scope.writePhotosAlbum',
                            success() {
                              console.log('授权成功')
                            }
                          })
                        }
                      }
                    })
                  } else {
                    wx.setStorageSync("openid", res.data.openid); //把获取到的数据保存到本地缓存中，便于其他页面使用
                    wx.showToast({
                      title: '请先完成手机绑定！',
                      icon: 'none'
                    })
                    setTimeout(function(){
                      wx.redirectTo({
                        url: '../home/home',
                      })
                    },1000)
                  }
                },
                function(res) {
                  wx.showToast({
                    title: '登录错误！',
                  })
                },
              )
            ) : (t.loginFailed());
          }
        });
      });
  },
  loginFailed: function() {
    wx.showModal({
      title: "提示",
      content: "登录失败!",
      showCancel: !1
    }), wx.hideLoading();
  },
})